{% extends "layout.html" %}
{% block title %}Batch Status{% endblock %}
{% block content %}
  <h1>Globals</h1>
  <div class="attributes">
    <div>instance ID: {{ instance_id }}</div>
    <div>ready cores: {{ ready_cores_mcpu / 1000 }}</div>
  </div>

  <div>
    {% if not frozen %}
    <form action="{{ base_path }}/freeze" method="POST">
      <input type="hidden" name="_csrf" value="{{ csrf_token }}">
      <button class="dangerous">
        Freeze
      </button>
    </form>
    {% else %}
    <form action="{{ base_path }}/unfreeze" method="POST">
      <input type="hidden" name="_csrf" value="{{ csrf_token }}">
      <button class="dangerous">
        Unfreeze
      </button>
    </form>
    {% endif %}
  </div>

<h1>Feature Flags</h1>
  <div>
    <form action="{{ base_path }}/configure-feature-flags" method="POST">
      <input type="hidden" name="_csrf" value="{{ csrf_token }}">
      <label for="compact_billing_tables">compact_billing_tables: </label>
      <input type="checkbox"
             id="compact_billing_tables"
             name="compact_billing_tables"
             {% if feature_flags['compact_billing_tables'] %}checked{% endif %}
      value="true" />
      <label for="oms_agent">oms_agent: </label>
      <input type="checkbox"
             id="oms_agent"
             name="oms_agent"
             {% if feature_flags['oms_agent'] %}checked{% endif %}
      value="true" />
      <div>
        <button>Update</button>
      </div>
    </form>
  </div>

  <h1>Instance Collections</h1>

  <table class="data-table" id="inst_colls">
    <thead>
    <tr>
      <th colspan="1">Name</th>
      <th colspan="4">Instances</th>
      <th></th>
      <th colspan="4">Cores</th>
      <th></th>
      <th colspan="3">Schedulable Cores</th>
    </tr>
    <tr>
      <th></th>
      <th>Pending</th>
      <th>Active</th>
      <th>Inactive</th>
      <th>Deleted</th>
      <th></th>
      <th>Pending</th>
      <th>Active</th>
      <th>Inactive</th>
      <th>Deleted</th>
      <th></th>
      <th>Free</th>
      <th>Total</th>
      <th>% Free</th>
    </tr>
    </thead>
    <tbody>
    {% for pool in pools %}
    <tr>
      <td><a href="{{ base_path }}/inst_coll/pool/{{ pool.name }}">{{ pool.name }}</a></td>
      <td class="numeric-cell">{{ pool.all_versions_instances_by_state['pending'] }}</td>
      <td class="numeric-cell">{{ pool.all_versions_instances_by_state['active'] }}</td>
      <td class="numeric-cell">{{ pool.all_versions_instances_by_state['inactive'] }}</td>
      <td class="numeric-cell">{{ pool.all_versions_instances_by_state['deleted'] }}</td>
      <td></td>
      <td class="numeric-cell">{{ pool.all_versions_cores_mcpu_by_state['pending'] / 1000 }}</td>
      <td class="numeric-cell">{{ pool.all_versions_cores_mcpu_by_state['active'] / 1000 }}</td>
      <td class="numeric-cell">{{ pool.all_versions_cores_mcpu_by_state['inactive'] / 1000 }}</td>
      <td class="numeric-cell">{{ pool.all_versions_cores_mcpu_by_state['deleted'] / 1000 }}</td>
      <td></td>
      <td class="numeric-cell">{{ pool.current_worker_version_stats.active_schedulable_free_cores_mcpu / 1000 }}</td>
      <td class="numeric-cell">{{ pool.current_worker_version_stats.cores_mcpu_by_state['active'] / 1000 }}</td>
      {% if pool.current_worker_version_stats.cores_mcpu_by_state['active'] != 0 %}
      <td class="numeric-cell">{{ (pool.current_worker_version_stats.active_schedulable_free_cores_mcpu * 100 / pool.current_worker_version_stats.cores_mcpu_by_state['active']) | round(1)}}%</td>
      {% else %}
      <td class="numeric-cell"></td>
      {% endif %}
    </tr>
    {% endfor %}
    <tr>
      <td><a href="{{ base_path }}/inst_coll/jpim">{{ jpim.name }}</a></td>
      <td class="numeric-cell">{{ jpim.all_versions_instances_by_state['pending'] }}</td>
      <td class="numeric-cell">{{ jpim.all_versions_instances_by_state['active'] }}</td>
      <td class="numeric-cell">{{ jpim.all_versions_instances_by_state['inactive'] }}</td>
      <td class="numeric-cell">{{ jpim.all_versions_instances_by_state['deleted'] }}</td>
      <td></td>
      <td class="numeric-cell">{{ jpim.all_versions_cores_mcpu_by_state['pending'] / 1000 }}</td>
      <td class="numeric-cell">{{ jpim.all_versions_cores_mcpu_by_state['active'] / 1000 }}</td>
      <td class="numeric-cell">{{ jpim.all_versions_cores_mcpu_by_state['inactive'] / 1000 }}</td>
      <td class="numeric-cell">{{ jpim.all_versions_cores_mcpu_by_state['deleted'] / 1000 }}</td>
      <td></td>
      <td class="numeric-cell"></td>
      <td class="numeric-cell"></td>
      <td class="numeric-cell"></td>
    </tr>
    </tbody>
    <tfoot>
    <tr>
      <td>Total</td>
      <td class="numeric-cell">{{ global_n_instances_by_state['pending'] }}</td>
      <td class="numeric-cell">{{ global_n_instances_by_state['active'] }}</td>
      <td class="numeric-cell">{{ global_n_instances_by_state['inactive'] }}</td>
      <td class="numeric-cell">{{ global_n_instances_by_state['deleted'] }}</td>
      <td></td>
      <td class="numeric-cell">{{ global_cores_mcpu_by_state['pending'] / 1000 }}</td>
      <td class="numeric-cell">{{ global_cores_mcpu_by_state['active'] / 1000 }}</td>
      <td class="numeric-cell">{{ global_cores_mcpu_by_state['inactive'] / 1000 }}</td>
      <td class="numeric-cell">{{ global_cores_mcpu_by_state['deleted'] / 1000 }}</td>
      <td></td>
      <td class="numeric-cell">{{ global_schedulable_free_cores_mcpu / 1000 }}</td>
      <td class="numeric-cell">{{ global_schedulable_cores_mcpu / 1000 }}</td>
      {% if global_schedulable_cores_mcpu != 0 %}
      <td class="numeric-cell">{{ (global_schedulable_free_cores_mcpu * 100 / global_schedulable_cores_mcpu) | round(1)}}%</td>
      {% else %}
      <td class="numeric-cell"></td>
      {% endif %}
    </tr>
    </tfoot>
  </table>

  <h1>Instances</h1>
  <table class="data-table" id="instances">
    <thead>
      <tr>
        <th>Name</th>
        <th>Instance Collection</th>
        <th>Location</th>
        <th>Version</th>
        <th>State</th>
        <th>Free Cores</th>
        <th>Failed Requests</th>
        <th>Time Created</th>
        <th>Last Updated</th>
      </tr>
    </thead>
    <tbody>
      {% for instance in instances %}
      <tr>
        <td>{{ instance.name }}</td>
        <td>{{ instance.inst_coll.name }}</td>
        <td>{{ instance.location }}</td>
        <td class="numeric-cell">{{ instance.version }}</td>
        <td>{{ instance.state }}</td>
        <td class="numeric-cell">{{ instance.free_cores_mcpu / 1000 }} / {{ instance.cores_mcpu / 1000 }}</td>
        <td class="numeric-cell">{{ instance.failed_request_count }}</td>
        <td>{{ instance.time_created_str() }}</td>
        <td>{{ instance.last_updated_str() }} ago</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
{% endblock %}
